package cn.wyj.org.controller;

import cn.wyj.basic.util.AjaxResult;
import cn.wyj.basic.util.PageList;
import cn.wyj.org.domain.Employee;
import cn.wyj.org.query.EmployeeQuery;
import cn.wyj.org.service.EmployeeService;
import cn.wyj.system.domain.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        if (null == employee.getId()){
            // 添加
            employeeService.add(employee);
        }else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工数据")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    public Employee findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 根据部门id查询一个部门所有的员工
     */
    @GetMapping("/dept/{id}")
    public List<Employee> findEmpByDeptId(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long deptId){
        return employeeService.findEmpByDeptId(deptId);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }



}
